//
//  HJRepairGroupHeaderView.m
//  GAMCAfterSales
//
//  Created by 贺佳 on 2018/7/30.
//  Copyright © 2018年 glendale. All rights reserved.
//

#import "HJRepairGroupHeaderView.h"
#import "popContentViewController.h"
#import "DDPopoverBackgroundView.h"

#import "HJCustomTextField.h"
#import "HJCustomSelectContentView.h"
#import "HJLocalDataManager.h"
#import "HJCommonModel.h"

@interface HJRepairGroupHeaderView()<ContentViewControllerDelegate>
{
    //下拉菜单的属性
    popContentViewController *_contentCon;
    UIPopoverController *_popCon;
}
/**
 代码
 */
@property(nonatomic,weak)HJCustomTextField*repairtGroupCodeField;
/**
 车型
 */
@property(nonatomic,weak)HJCustomSelectContentView*repairtCarTypeBtn;
/**
 名称
 */
@property(nonatomic,weak)HJCustomTextField*repairtGroupNameField;
/**
 车型组缓存数据
 */
@property(nonatomic,strong)NSMutableArray*arrDisDataForCarType;
@end
@implementation HJRepairGroupHeaderView
/** 维修车型组 */
-(NSMutableArray *)arrDisDataForCarType{
    if (_arrDisDataForCarType == nil) {
        NSMutableArray *arr = [HJLocalDataManager sharedInstance].cacheDict[@"TM_MODEL_LABOUR"];
        _arrDisDataForCarType=[HJCommonModel mj_objectArrayWithKeyValuesArray:arr];
    }
    return _arrDisDataForCarType;
}
InitFrameM
-(void)setUI{
    HJCustomSelectContentView*repairtCarTypeBtn=[[HJCustomSelectContentView alloc]init];
    self.repairtCarTypeBtn=repairtCarTypeBtn;
    repairtCarTypeBtn.titleContent=@"维修车型组";
    [self addSubview:repairtCarTypeBtn];
    [repairtCarTypeBtn addTarget:self action:@selector(clickSelectCartype:) forControlEvents:UIControlEventTouchUpInside];
    
    HJCustomTextField*repairtGroupCodeField=[[HJCustomTextField alloc]init];
    repairtGroupCodeField.titleContent=@"维修组合代码";
    [self addSubview:repairtGroupCodeField];
    [repairtGroupCodeField.contentTextField addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
    self.repairtGroupCodeField=repairtGroupCodeField;
    
    HJCustomTextField*repairtGroupNameField=[[HJCustomTextField alloc]init];
    repairtGroupNameField.titleContent=@"维修组合名称";
    self.repairtGroupNameField=repairtGroupNameField;
    [self addSubview:repairtGroupNameField];
     [repairtGroupNameField.contentTextField addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
    
    WEAKSELF;
    [self.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:0 tailSpacing:0];
    [self.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf);
        make.height.mas_equalTo(30);
    }];
    
}
-(void)changeText:(UITextField*)textField{
    if (textField==self.repairtGroupCodeField.contentTextField) {
        if ([_delegate respondsToSelector:@selector(repairGroupHeaderView:changeRepairtCode:)]) {
            [_delegate repairGroupHeaderView:self changeRepairtCode:textField.text];
        }
    }else if (textField==self.repairtGroupNameField.contentTextField){
        if ([_delegate respondsToSelector:@selector(repairGroupHeaderView:changeRepairtName:)]) {
            [_delegate repairGroupHeaderView:self changeRepairtName:textField.text];
        }
    }
}
-(void)clickSelectCartype:(UIButton*)btn{
    //创建下拉菜单控件
    [self createThePopViewControl:btn];
    NSMutableArray*arr=[NSMutableArray array];
    for (HJCommonModel*model in self.arrDisDataForCarType) {
        [arr addObject:model.NAME];
    }
    _contentCon.list =arr;
    [_contentCon.myTableView reloadData];
}
//创建下拉菜单
-(void)createThePopViewControl:(UIButton *)sender
{
    if (!_contentCon)
    {
        _contentCon=[[popContentViewController alloc] initWithNibName:@"popContentViewController" bundle:nil];
    }
    
    _contentCon.delegate = self;
    _contentCon.btnSender=sender;
    
    _popCon = [[UIPopoverController alloc] initWithContentViewController:_contentCon];
    [DDPopoverBackgroundView setTintColor:[UIColor colorWithRed:234 green:248 blue:222 alpha:0.9]];
    [DDPopoverBackgroundView setArrowBase:30.0];//箭头
    [DDPopoverBackgroundView setArrowHeight:15.0];
    [DDPopoverBackgroundView setContentInset:1.0];
    
    [_popCon setPopoverBackgroundViewClass:[DDPopoverBackgroundView class]];
    [_popCon setPopoverContentSize:CGSizeMake(sender.frame.size.width, 280)];
    
    [_popCon presentPopoverFromRect:CGRectMake(40, 15, 70, 20) inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

#pragma mark - 下拉框选中行回调方法
-(void)selectDelegate:(NSInteger)row btn:(UIButton *)btn
{
    
    if (btn == self.repairtCarTypeBtn.selectBtn)
    {
        HJCommonModel*model=self.arrDisDataForCarType[row];
        if ([_delegate respondsToSelector:@selector(repairGroupHeaderView:changeRepairtCarType:)]) {
            [_delegate repairGroupHeaderView:self changeRepairtCarType:model.CODE];
        }
         [btn setTitle:model.NAME forState:UIControlStateNormal];
    }
    //清空下拉菜单的数据，并dismiss掉下拉菜单
    _contentCon.list = nil;
    [_popCon dismissPopoverAnimated:YES];
    _popCon = nil;
}

@end
